package com.zyb.beidada.config;

import io.reactivex.Scheduler;
import io.reactivex.schedulers.Schedulers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 十八岁讨厌编程
 * @date 2025/5/9 21:51
 * @PROJECT_NAME beidada-backend
 * @description 定制VIP用户的线程池
 */

@Configuration
public class VipSchedulerConfig {


    @Bean
    public Scheduler getVipScheduler() {
        ThreadFactory threadFactory = new ThreadFactory() {

            private final AtomicInteger threadNumber = new AtomicInteger(1);

            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r,"VIP-Thread-" + threadNumber.getAndIncrement());
                //设为非守护线程，主线程结束了，该线程不会结束
                thread.setDaemon(false);
                return thread;
            }
        };
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10, threadFactory);
        return Schedulers.from(scheduledExecutorService);
    }
}
